//
//  myView.m
//  CollectionScollView
//
//  Created by 苗钧豪 on 16/8/4.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "myView.h"
#import "myCollectionViewCell.h"

@implementation myView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _array = [NSArray arrayWithObjects:@"CD",@"CD",@"CJ",@"GB",@"TC",@"TM",@"TM", nil];
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        _cView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
        layout.itemSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _cView.delegate = self;
        _cView.dataSource = self;
        _cView.backgroundColor = [UIColor whiteColor];
        _cView.pagingEnabled = YES;
        [self addSubview:_cView];
        [_cView registerNib:[UINib nibWithNibName:@"myCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"1"];
        _cView.contentOffset = CGPointMake(self.bounds.size.width, 0);
        _page = [[UIPageControl alloc]initWithFrame:CGRectMake((self.bounds.size.width / 2) - 50, self.bounds.size.height - 50, 100, 50)];
        _page.pageIndicatorTintColor = [UIColor lightGrayColor];
        _page.currentPageIndicatorTintColor = [UIColor blackColor];
        _page.numberOfPages = 5;
        [_page addTarget:self action:@selector(page:) forControlEvents:(UIControlEventValueChanged)];
        [self addSubview:_page];
    }
    return self;
}
//- (instancetype)initWithCoder:(NSCoder *)aDecoder {
//    self = [super initWithCoder:aDecoder];
//    if (self) {
//        self.backgroundColor = [UIColor redColor];
//    }
//    return self;
//}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _array.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    myCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"1" forIndexPath:indexPath];
    cell.imgview.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",_array[indexPath.row]]];
    return cell;
}
- (void)page:(UIPageControl *)page {
    [page updateCurrentPageDisplay];
    _cView.contentOffset = CGPointMake(page.currentPage * self.bounds.size.width, 0);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    _page.currentPage = (_cView.contentOffset.x / self.bounds.size.width) - 1;
    if (_cView.contentOffset.x > (self.bounds.size.width * 5)) {
        _cView.contentOffset = CGPointMake(0, 0);
        _page.currentPage = 0;
    }
    if (_cView.contentOffset.x < self.bounds.size.width) {
        _cView.contentOffset = CGPointMake(self.bounds.size.width * 5, 0);
        _page.currentPage = 4;
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
